package aes.client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import common.CommandMessage;

public class ExamExecutionStudentUI extends JPanel implements ActionListener, Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7512645960064731284L;

	private ClientUI ui;
	private Client c;
	private JButton btnBack; 
	private JButton btnStart;
	private JTextField textCode;
	private JTextField txtID;
	
	public ExamExecutionStudentUI(Client c,ClientUI ui){
		super();
		this.c = c;
		this.ui = ui;

		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.getContentPane().add(this);
		
		c.addObserver(this);
		setLayout(new GridBagLayout());

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));
		
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowHeights = new int[]{0, 145, 161, 0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		JLabel lblExamExecTeacherTitle = new JLabel("Exam Execution:");
		lblExamExecTeacherTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblExamExecTeacherTitle.setVerticalAlignment(SwingConstants.TOP);
		lblExamExecTeacherTitle.setBounds(7, 10, 70, 17);
		lblExamExecTeacherTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		GridBagConstraints gbc_lblExamExecTeacherTitle = new GridBagConstraints();
		gbc_lblExamExecTeacherTitle.anchor = GridBagConstraints.NORTH;
		gbc_lblExamExecTeacherTitle.gridwidth = 2;
		gbc_lblExamExecTeacherTitle.fill = GridBagConstraints.BOTH;
		gbc_lblExamExecTeacherTitle.insets = new Insets(10, 0, 5, 0);
		gbc_lblExamExecTeacherTitle.gridx = 0;
		gbc_lblExamExecTeacherTitle.gridy = 0;
		panel.add(lblExamExecTeacherTitle, gbc_lblExamExecTeacherTitle);
		
		JLabel lblId = new JLabel("ID:");
		GridBagConstraints gbc_lblId = new GridBagConstraints();
		gbc_lblId.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblId.insets = new Insets(0, 0, 8, 5);
		gbc_lblId.gridx = 0;
		gbc_lblId.gridy = 1;
		panel.add(lblId, gbc_lblId);
		
		txtID = new JTextField();
		GridBagConstraints gbc_txtID = new GridBagConstraints();
		gbc_txtID.anchor = GridBagConstraints.SOUTH;
		gbc_txtID.insets = new Insets(0, 0, 5, 10);
		gbc_txtID.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtID.gridx = 1;
		gbc_txtID.gridy = 1;
		panel.add(txtID, gbc_txtID);
		txtID.setColumns(10);
		
		JLabel lblCode = new JLabel("Code:");
		lblCode.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblCode = new GridBagConstraints();
		gbc_lblCode.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblCode.insets = new Insets(3, 0, 5, 5);
		gbc_lblCode.gridx = 0;
		gbc_lblCode.gridy = 2;
		panel.add(lblCode, gbc_lblCode);
		
		textCode = new JTextField();
		textCode.setHorizontalAlignment(SwingConstants.LEADING);
		GridBagConstraints gbc_textCode = new GridBagConstraints();
		gbc_textCode.anchor = GridBagConstraints.NORTH;
		gbc_textCode.insets = new Insets(0, 0, 5, 10);
		gbc_textCode.fill = GridBagConstraints.HORIZONTAL;
		gbc_textCode.gridx = 1;
		gbc_textCode.gridy = 2;
		panel.add(textCode, gbc_textCode);
		MaxLengthTextDocument maxLength = new MaxLengthTextDocument();
		maxLength.setMaxChars(5);
		textCode.setDocument(maxLength);
		textCode.setColumns(10);
		
		btnBack = new JButton("Back", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/back.png")));
		btnBack.addActionListener(this); 
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.WEST;
		gbc_btnBack.insets = new Insets(5, 10, 10, 5);
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 3;
		panel.add(btnBack, gbc_btnBack);
		btnBack.setBounds(353, 7, 57, 23);

		btnStart = new JButton("Start", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/execute.png")));
		btnStart.addActionListener(this); 
		GridBagConstraints gbc_btnStart = new GridBagConstraints();
		gbc_btnStart.insets = new Insets(5, 5, 10, 10);
		gbc_btnStart.anchor=GridBagConstraints.EAST;
		gbc_btnStart.gridx = 1;
		gbc_btnStart.gridy = 3;
		panel.add(btnStart, gbc_btnStart);
		btnStart.setBounds(353, 7, 57, 23);
		ui.getRootPane().setDefaultButton(btnStart);
	}
	
	
	
	
	@SuppressWarnings("static-access")
	@Override
	public void update(Observable o, Object arg1) {
		if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.START_EXAM_EXECUTION)) {
				if (ms.getParams()[1] != null && Integer.parseInt(ms.getParams()[1].toString())==-2) {
					ui.ShowMessageBox("ERROR", "Can't view exam until it is checked.", JOptionPane.ERROR_MESSAGE);
					return;
				}
				else if (ms.getParams()[1] == null || Integer.parseInt(ms.getParams()[1].toString())<1) {
					// Wrong code.
					ui.ShowMessageBox("ERROR", "Can't find exam. Please check your code", JOptionPane.ERROR_MESSAGE);
					return; 
				}
				else
				{
					if (Integer.parseInt(ms.getParams()[1].toString())==1) {
						// Start computorized exam.
						ui.SetContent(ClientUI.EXAM_EXECUTION_FORM_SCREEN);
						Container cont = ui.getContentPane();
						if (cont instanceof ExamExecutionFormUI) {
							ExamExecutionFormUI form = (ExamExecutionFormUI)cont;
							form.loadExam(textCode.getText().trim(), ui.getUser().getId());
						}
						else
							ui.ShowLastScreen();
					}
					else if (Integer.parseInt(ms.getParams()[1].toString())==2) {
						// Start manual exam. 
						ui.SetContent(ClientUI.EXAM_EXECUTION_MANUAL_FORM_SCREEN);
						Container cont = ui.getContentPane();
						if (cont instanceof ManualExamExecutionFormUI) {
							ManualExamExecutionFormUI form = (ManualExamExecutionFormUI)cont;
							form.loadExam(textCode.getText().trim(), ui.getUser().getId());
						}
						else
							ui.ShowLastScreen();
					}
					else
						ui.ShowLastScreen();
				}
			}
		}
	}

	@SuppressWarnings({ "static-access" })
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnBack)) {
			ui.SetContent(ClientUI.MAIN_SCREEN);
		
		}
		else if (e.getSource().equals(btnStart)) {
			if (textCode.getText().trim().length() != 4) {
				ui.ShowMessageBox("ERROR", "Please fill execution code 4 characters long", JOptionPane.ERROR_MESSAGE);
				return; 
			}
			if (!txtID.getText().trim().equals(Integer.toString(ui.getUser().getId()))) {
				ui.ShowMessageBox("ERROR", "You have entered wrong ID", JOptionPane.ERROR_MESSAGE);
				return;
			}
			// Start exam.
			CommandMessage msg = new CommandMessage(CommandMessage.START_EXAM_EXECUTION, new String[] {textCode.getText().trim(), Integer.toString(ui.getUser().getId())});
			try {
				c.sendToServer(msg);
			} catch (IOException ex) {
				ex.printStackTrace();
				ui.ShowMessageBox("Error", "Can't retrieve exams list", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	

}
